package com.sn.dao;

import com.sn.model.Customer;
import com.sn.model.VipUser;
import com.sn.model.req.CustomerReq;
import com.sn.model.req.VipUserReq;
import com.sn.model.vo.CustomerVo;
import com.sn.model.vo.VipUserVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by qingshengzheng on 18/6/20.
 */
@Mapper
public interface UserMapper {

    @Insert("insert into customer(mobile,createtime,truename,updatetime,sex,vipuid,usertype,cardno,comments,password) values(" +
            "#{mobile},#{createtime},#{truename},#{updatetime},#{sex},#{vipuid},#{usertype},#{cardno},#{comments},#{password})")
    @Options(useGeneratedKeys=true, keyProperty="cid", keyColumn="cid")
    public void addCustomer(Customer customer);

    @Update("<script>" +
            "update customer" +
            "<set>" +
            "<if test=\"mobile!=null and mobile !=''\">mobile=#{mobile},</if>" +
            "<if test=\"truename!=null and truename !=''\">truename=#{truename},</if>" +
            "<if test=\"sex!=null and sex !=''\">sex=#{sex},</if>" +
            "<if test=\"vipuid!=null and vipuid !=''\">vipuid=#{vipuid},</if>" +
            "<if test=\"usertype!=null and usertype !=''\">usertype=#{usertype},</if>" +
            "<if test=\"cardno!=null and cardno !=''\">cardno=#{cardno},</if>" +
            "<if test=\"comments!=null and comments !=''\">comments=#{comments},</if>" +
            "</set>" +
            "where cid=#{cid}" +
            "</script>")
    public void updateCustomer(Customer customer);

    @Select("select * from customer where truename=#{truename}")
    public CustomerVo queryCustomerByTruename(String truename);


    @Delete("delete from customer where cid=#{id} ")
    public void deleteCustomer(Long id);

    @Select("<script>select * from customer as a " +
            "<where>" +
            "<if test=\"cid!=null and cid !=''\">and a.cid=#{cid} </if>" +
            "<if test=\"mobile!=null and mobile !=''\">and a.mobile=#{mobile} </if>" +
            "<if test=\"truename!=null and truename !=''\">and a.truename like  concat(#{truename},'%') </if>" +
            "<if test=\"sex!=null and sex !=''\">and a.sex=#{sex} </if>" +
            "<if test=\"vipuid!=null and vipuid !=''\">and a.vipuid=#{vipuid} </if>" +
            "<if test=\"usertype!=null and usertype !=''\">and a.usertype=#{usertype} </if>" +
            "</where>" +
            "</script>")
    public List<CustomerVo>  queryCustomers(CustomerReq customerReq);



    @Select("select cid,truename from customer where truename like  concat(#{truename},'%') and usertype")
    public List<Customer> queryVipSearchList(@Param("truename")String truename,@Param("usertype") Integer usertype);


    @Update("update customer set password=#{password} where cid=#{cid}")
    public void updatePwd(@Param("cid")Long cid,@Param("password")String password);

    @Select("select * from customer as a where a.cid=#{cid}")
    public CustomerVo detail(Long cid);


}
